package com.hz.web.controller;

import com.hz.web.entity.Employee;
import com.hz.web.entity.RoleEntity;
import com.hz.web.entity.UserEntity;
import com.hz.web.service.IemployeeService;
import com.hz.web.service.IroleService;
import com.hz.web.service.IuserService;
import com.hz.web.util.ParmsEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Resource
    private IemployeeService employeeService;


    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("queryEmployee")
    @ResponseBody
    public Map<String, Object> queryEmployee(ParmsEntity entity, Integer userId,String issued) {
        Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
        int page = entity.getPage();
        int rows = entity.getRows();
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("userName", entity.getUserName());
            parmsMap.put("end", end);
            parmsMap.put("useId",userId);
            parmsMap.put("obtainingCertificate", issued);
        }

        List<Employee> list = new ArrayList<Employee>();

        int count = 0;
        list = employeeService.queryEmployeeList(parmsMap);
        count = employeeService.queryEmployeeCount(parmsMap);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (list.size() > 0) {
            for (Employee employee : list) {
                if (employee.getCreateTime() != null) {
                    employee.setCreateTimeStr(sdf.format(employee.getCreateTime()));
                }
            }
        }

        resultMap.put("rows", list);
        resultMap.put("total", count);
        return resultMap;
    }




    /**
     * 查询获证人员
     *
     * @return
     */
    @RequestMapping("queryEmployeeCertificate")
    @ResponseBody
    public Map<String, Object> queryEmployeeCertificate(ParmsEntity entity, Integer userId,String issued) {
        Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
        Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
        int page = entity.getPage();
        int rows = entity.getRows();
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("userName", entity.getUserName());
            parmsMap.put("end", end);
            parmsMap.put("useId",userId);
            parmsMap.put("obtainingCertificate", issued);
        }

        List<Employee> list = new ArrayList<Employee>();

        int count = 0;
        list = employeeService.queryEmployeeCertificate(parmsMap);
        count = employeeService.queryEmployeeCertificateCount(parmsMap);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (list.size() > 0) {
            for (Employee employee : list) {
                if (employee.getCreateTime() != null) {
                    employee.setCreateTimeStr(sdf.format(employee.getCreateTime()));
                }
            }
        }

        resultMap.put("rows", list);
        resultMap.put("total", count);
        return resultMap;
    }


    /**
     * 新增
     *
     * @return
     */
    @RequestMapping("insertEmployee")
    @ResponseBody
    public Map<String, Object> insertEmployee(HttpSession session,Employee entity) {
        Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
        int row = -1;
        String username = "";
        int company =-1;
        if (entity.getUsername() != null) {
            username = entity.getUsername().replaceAll(" ", "");
            Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
            parmsMap.put("name", username);
            List<Employee> list;
            list = employeeService.queryEmployeeList(parmsMap);
            if (list.size() > 0) {
                resultMap.put("row", row);
                resultMap.put("msg", "此员工信息已存在！");
                return resultMap;
            }
        } else {
            resultMap.put("row", row);
            resultMap.put("msg", "员工姓名为必填字段！");
            return resultMap;
        }
        if(session.getAttribute("userinfo")!=null){
            UserEntity user = (UserEntity) session.getAttribute("userinfo");
            company = user.getId();
        }

        entity.setUsername(username);
        entity.setSex(entity.getSex().replaceAll(" ", ""));
        entity.setIdNum(entity.getIdNum());
        entity.setCreateTime(new Date());
        entity.setEducation(entity.getEducation());
        entity.setMajor(entity.getMajor());
        entity.setPhoneNum(entity.getPhoneNum());
        entity.setIsDel("0");
        entity.setObtainingCertificate("0");
        entity.setCompanyId(company);

        try {
            row = employeeService.insert(entity);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("row", row);
            resultMap.put("msg", "添加异常");
            return resultMap;
        }
        resultMap.put("row", row);
        resultMap.put("msg", "添加成功！");
        return resultMap;
    }


    /**
     * 删除员工信息
     *
     * @param entity
     * @return
     */
    @RequestMapping("delEmployee")
    @ResponseBody
    public Map<String, Object> delEmployee(Employee entity) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        int id = entity.getId();

        int delrow = -1;
        try {
            Employee employee = new Employee();
            employee.setId(entity.getId());
            employee.setIsDel("1");
            delrow = employeeService.updateEmployeeById(employee);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg", "删除异常！");
            resultMap.put("row", delrow);
        }
        if (delrow >= 0) {
            resultMap.put("msg", "删除成功！");
        } else {
            resultMap.put("msg", "删除失败！");
        }

        resultMap.put("row", delrow);
        return resultMap;
    }

    /**
     * 修改权限信息
     *
     * @param entity
     * @return
     */
    @RequestMapping("updateEmployee")
    @ResponseBody
    public Map<String, Object> updateEmployee(Employee entity) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        Map<String, Object> parmsMap = new HashMap<String, Object>();
        parmsMap.put("roleId", entity.getId());

        int row = -1;
        try {
            row = employeeService.updateEmployeeById(entity);//只修改权限信息

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg", "修改信息异常！");
        }
        if (row != -1) {
            resultMap.put("msg", "修改权限成功！");
        }
        resultMap.put("row", row);
        return resultMap;
    }
}
